package com.orhon.smartcampus.modules.course.mutation;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.DictionaryDao;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.dao.SubjectsDao;
import com.orhon.smartcampus.modules.base.entity.Dictionary;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.entity.Subjects;
import com.orhon.smartcampus.modules.course.dao.CoursesDao;
import com.orhon.smartcampus.modules.course.entity.Courses;
import com.orhon.smartcampus.modules.course.gqlInputModel.GQLCoursesInput;
import com.orhon.smartcampus.modules.course.inputMapper.CoursesInputMapper;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AddCoursesMutation implements DataFetcher {


    @Autowired
    CoursesDao coursesDao;

    @Autowired
    DictionaryDao dictionaryDao;

    @Autowired
    SchoolsDao schoolDao;

    @Autowired
    SubjectsDao subjectDao;

    /**
     * 新增/修改课程
     * editor : zy
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        GQLCoursesInput input = CoursesInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Courses courses = null;
        //判断是添加课程还是修改课程
        if (input.getId() != null) {
            //id不为空则修改课程
            courses = coursesDao.get(Courses.class, input.getId());
            courses.setUpdatedAt(new Date());
        } else {
            //id为空则新加课程
            courses = new Courses();
            courses.setCreatedAt(new Date());
        }
        //添加课程名称
        if(input.getCourseName() != null){
            courses.setCourseName(JSON.toJSONString(input.getCourseName()));
        }
        //课程别名
        if(input.getCourseSlug() != null){
            courses.setCourseSlug(input.getCourseSlug());
        }
        //课程分类
       if(input.getCourseKindDict() != null){
           courses.setCourseKindDict(dictionaryDao.get(Dictionary.class, input.getCourseKindDict()));
       }
       //课程级别
       if(input.getLevel() != null){
           courses.setLevel(dictionaryDao.get(Dictionary.class, input.getLevel()));
       }
       //课程所属学校
       if(input.getSchool() != null){

           courses.setSchoolId(schoolDao.get(Schools.class, input.getSchool()));
       }
       //课程所属学科
        if(input.getSubject() != null){

            courses.setSubjectId(subjectDao.get(Subjects.class, input.getSubject()));
        }
        //课程编码
        if(input.getCourseCode() != null){

            courses.setCourseCode(input.getCourseCode());
        }
        //课程类型（administrative：行政，walking：走班）
        if(input.getType() != null){
            courses.setType(input.getType());
        }
        try {
            coursesDao.saveOrUpdate(courses);
        } catch (Exception e) {
            return Result.error(500, "" + e);
        }
        return Result.ok();
    }
}

